package wireless.loadrec;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import wireless.asserrec.pojo.AErrecog;
import wireless.assrec.pojo.ARecog;
import wireless.strerrec.pojo.SErrecog;
import wireless.strrec.pojo.SRecog;
import wireless.util.GetAndSave;

public class LoadRec {

	private String file1;
	private String file2;
	private String file3;
	private String file4;
	public String getFile1() {
		return file1;
	}
	public void setFile1(String file1) {
		this.file1 = file1;
	}
	public String getFile2() {
		return file2;
	}
	public void setFile2(String file2) {
		this.file2 = file2;
	}
	public String getFile3() {
		return file3;
	}
	public void setFile3(String file3) {
		this.file3 = file3;
	}
	public String getFile4() {
		return file4;
	}
	public void setFile4(String file4) {
		this.file4 = file4;
	}
	
	public String execute() throws Exception{

		List<SRecog> list1 = findAllSR(file1);
		List<SErrecog> list2 = findAllSER(file2);
		List<ARecog> list3 = findAllAR(file3);
		List<AErrecog> list4 = findAllAER(file4);
		
		GetAndSave<SRecog> dao1 =new GetAndSave<SRecog>("SRecog");
		dao1.saveEntityList(list1);
		
		GetAndSave<SErrecog> dao2 =new GetAndSave<SErrecog>("SErrecog");
		dao2.saveEntityList(list2);
		
		GetAndSave<ARecog> dao3 =new GetAndSave<ARecog>("ARecog");
		dao3.saveEntityList(list3);
		
		GetAndSave<AErrecog> dao4 =new GetAndSave<AErrecog>("AErrecog");
		dao4.saveEntityList(list4);
		
	
	return "success";
	}
	
	
	private List<SRecog> findAllSR(String filePath){
	 	File file = new File(filePath);
        List<SRecog> list = new ArrayList<SRecog>();
        BufferedReader reader = null;
        try {
            System.out.println("以行为单位读取文件内容，一次读一整行：");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 0;
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                // 显示行号
                System.out.println("line " + line + ": " + tempString);
                SRecog sr = new SRecog();
                sr.setFid(Integer.parseInt(tempString.split(" ")[0]));
                sr.setFrame(Integer.parseInt(tempString.split(" ")[1]));
                sr.setRecog(Integer.parseInt(tempString.split(" ")[2]));
                sr.setPercent(Float.parseFloat(tempString.split(" ")[3]));
                
                list.add(sr);
                line++;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        return list;
	}
	
	private List<SErrecog> findAllSER(String filePath){
	 	File file = new File(filePath);
        List<SErrecog> list = new ArrayList<SErrecog>();
        BufferedReader reader = null;
        try {
            System.out.println("以行为单位读取文件内容，一次读一整行：");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 0;
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                // 显示行号
                System.out.println("line " + line + ": " + tempString);
                SErrecog ser = new SErrecog();
                ser.setFid(Integer.parseInt(tempString.split(" ")[0]));
                ser.setFrame(Integer.parseInt(tempString.split(" ")[1]));
                ser.setErrecog(Integer.parseInt(tempString.split(" ")[2]));
                ser.setPercent(Float.parseFloat(tempString.split(" ")[3]));
                
                list.add(ser);
                line++;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        return list;
	}
	
	private List<ARecog> findAllAR(String filePath){
	 	File file = new File(filePath);
        List<ARecog> list = new ArrayList<ARecog>();
        BufferedReader reader = null;
        try {
            System.out.println("以行为单位读取文件内容，一次读一整行：");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 0;
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                // 显示行号
                System.out.println("line " + line + ": " + tempString);
                ARecog ar = new ARecog();
                ar.setAid(Integer.parseInt(tempString.split(" ")[0]));
                ar.setFrame(Integer.parseInt(tempString.split(" ")[1]));
                ar.setRecog(Integer.parseInt(tempString.split(" ")[2]));
                ar.setPercent(Float.parseFloat(tempString.split(" ")[3]));
                
                list.add(ar);
                line++;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        return list;
	}
	
	private List<AErrecog> findAllAER(String filePath){
	 	File file = new File(filePath);
        List<AErrecog> list = new ArrayList<AErrecog>();
        BufferedReader reader = null;
        try {
            System.out.println("以行为单位读取文件内容，一次读一整行：");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 0;
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                // 显示行号
                System.out.println("line " + line + ": " + tempString);
                AErrecog aer = new AErrecog();
                aer.setAid(Integer.parseInt(tempString.split(" ")[0]));
                aer.setFrame(Integer.parseInt(tempString.split(" ")[1]));
                aer.setErrecog(Integer.parseInt(tempString.split(" ")[2]));
                aer.setPercent(Float.parseFloat(tempString.split(" ")[3]));
                
                list.add(aer);
                line++;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        return list;
	}
	
}
